﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;
using DevExpress.XtraExport;

namespace THSERP.Modules.Report
{
    public partial class RP002 : ReportForm
    {
        public THSList<ARInvoiceItemsInfo> lstInvoiceItem;
        int ICProductGroupID = 0;
       
        public RP002()
        {
            InitializeComponent();
            lstInvoiceItem = new THSList<ARInvoiceItemsInfo>();
        }

        private void fld_btnShowProductGroupTreeForm_Click(object sender, EventArgs e)
        {
            int departmentID = Convert.ToInt32(fld_lkeFK_ICDepartmentID.EditValue);
            guiProductGroupTree guiProductGroupTree = new guiProductGroupTree(departmentID);
            guiProductGroupTree.Module = this.Module;
            if (guiProductGroupTree.ShowDialog() == DialogResult.OK)
            {
                ICProductGroupID = ((ICProductGroupsInfo)guiProductGroupTree.TreeList.GetSelectedObject()).ICProductGroupID;
                fld_txtFK_ICProductGroup.Text = guiProductGroupTree.GetSelectedProductGroupName();
                fld_lkeFK_ICDepartmentID.EditValue = guiProductGroupTree.ICDepartmentID;
            }
        }

        private void RP002_Load(object sender, EventArgs e)
        {
            fld_dteSearchFromARInvoiceDate.DateTime=THSApp.WorkingDate;
            fld_dteSearchToARInvoiceDate.DateTime=THSApp.WorkingDate;
            fld_lkeFK_APSupplierID.Screen = this;
            fld_lkeFK_APSupplierID.InitializeControl();
            fld_lkeFK_ICDepartmentID.Screen = this;
            fld_lkeFK_ICDepartmentID.InitializeControl();
            fld_dgcARRP002InvoiceItems.Screen = this;
            fld_dgcARRP002InvoiceItems.InitializeControl();
           // fld_lblBranchName.Text = ((BRBranchsInfo)new BRBranchsController().GetObjectByID(THSApp.CurrentCompanyInfo.FK_BRBranchID)).BRBranchName;
            fld_btnApply_Click(new object(), new EventArgs());
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_btnApply_Click(object sender, EventArgs e)
        {
            lstInvoiceItem.Clear();
            DateTime from = Convert.ToDateTime(fld_dteSearchFromARInvoiceDate.EditValue);
            DateTime to = Convert.ToDateTime(fld_dteSearchToARInvoiceDate.EditValue);
            int deparmentID = Convert.ToInt32(fld_lkeFK_ICDepartmentID.EditValue);
            int productGroupID = ICProductGroupID;
            int supplierID = Convert.ToInt32(fld_lkeFK_APSupplierID.EditValue);

            String query = ((Report.ReportModule)this.Module).GenerateInvoiceItemSearchQuery(from, to, deparmentID, productGroupID, supplierID, THSApp.CurrentCompanyInfo.FK_BRBranchID, "ii.FK_ICDepartmentID, ii.FK_ICProductGroupID, ii.FK_ICProductID, ii.FK_ICProductAttributeID,ii.ARInvoiceItemProductUnitPrice");
            ARInvoiceItemsController objInvoiceItemsController = new ARInvoiceItemsController();
            DataSet ds = objInvoiceItemsController.GetDataSet(query);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                ARInvoiceItemsInfo objInvoiceItemsInfo = (ARInvoiceItemsInfo)objInvoiceItemsController.GetObjectFromDataRow(row);
                ICProductsInfo objProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(objInvoiceItemsInfo.FK_ICProductID);
                objInvoiceItemsInfo.ARInvoiceItemProductDesc = objProductsInfo.ICProductDesc;
                objInvoiceItemsInfo.ARInvoiceItemProductAttribute = new ICProductAttributesController().GetAttributeValueByProductAttributeID(objInvoiceItemsInfo.FK_ICProductAttributeID);
                APSuppliersInfo objSuppliersInfo = (APSuppliersInfo)new APSuppliersController().GetObjectByID(objProductsInfo.FK_APSupplierID);
                if (objSuppliersInfo != null)
                    objInvoiceItemsInfo.ARInvoiceItemSupplierName = objSuppliersInfo.APSupplierName;
                lstInvoiceItem.Add(objInvoiceItemsInfo);
            }
            fld_dgcARRP002InvoiceItems.DataSource = lstInvoiceItem;
            fld_dgcARRP002InvoiceItems.RefreshDataSource();
            ((DevExpress.XtraGrid.Views.Grid.GridView)fld_dgcARRP002InvoiceItems.Views[0]).BestFitColumns();
        }

        private void fld_btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_btnExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSave = new SaveFileDialog();
            dlgSave.Filter = "Microsoft Excel|*.xls";
            DialogResult dlgResult = dlgSave.ShowDialog();
            if (dlgResult == DialogResult.OK)
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
               // this.fld_dgcARRP002InvoiceItems.ExportToXls(dlgSave.FileName);
                this.FindForm().Refresh();
                ExportXlsProvider provider = new ExportXlsProvider(dlgSave.FileName);
                DevExpress.XtraGrid.Export.BaseExportLink link = this.fld_dgcARRP002InvoiceItems.Views[0].CreateExportLink(provider);
                (link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
                link.ExportTo(true);
                provider.Dispose();
                Cursor.Current = currentCursor;
                if (DialogResult.Yes == MessageBox.Show("Xuất file Excel thành công! Bạn có muốn mở file ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    System.Diagnostics.Process.Start(dlgSave.FileName);
                }
            }
        }

        private void fld_btnPrint_Click(object sender, EventArgs e)
        {
            //this.fld_dgcARRP002InvoiceItems.PrintPriview();
            DevExpress.XtraPrinting.PrintingSystem printingSystem = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();

            try
            {
                //Set to landscape
                printingSystem.PageSettings.Landscape = true;
                this.Cursor = Cursors.WaitCursor;
                printLink.Component = this.fld_dgcARRP002InvoiceItems;
                printLink.CreateDocument(printingSystem);
                printLink.VerticalContentSplitting = DevExpress.XtraPrinting.VerticalContentSplitting.Exact;
                printingSystem.PreviewFormEx.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
                printingSystem.Dispose();
                printLink.Dispose();
            }

        }
    }
}